package zhangqi.com.mymp3;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class MainActivity extends AppCompatActivity {

    static Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bundle bundle = msg.getData();
            int duration = bundle.getInt("duration");
            int currentPosition = bundle.getInt("currentPosition");
            sb.setMax(duration);
            sb.setProgress(currentPosition);

        }
    };

    private MusicServiceConn conn;
    MusicInterface mi;
    private static SeekBar sb;
    private Intent intent;
    private ListView lv ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sb = (SeekBar) findViewById(R.id.sb);
        sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int progress = seekBar.getProgress();
                mi.seekTo(progress);
            }
        });

        intent = new Intent(this,MusicService.class);
        startService(intent);
        conn = new MusicServiceConn();
        bindService(intent, conn, BIND_AUTO_CREATE);

        String[] object = new String []{"小水果","最炫名族风","小苹果","月亮之上","我从草原来"};
        lv = (ListView) findViewById(R.id.lv);
        List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
        Map<String,Object> map1 = new HashMap<String,Object>();
        map1.put("photo", R.drawable.g1);
        map1.put("name", object[0]);
        data.add(map1);
        Map<String,Object> map2 = new HashMap<String,Object>();
        map2.put("photo", R.drawable.g2);
        map2.put("name", object[1]);
        data.add(map2);
        Map<String,Object> map3 = new HashMap<String,Object>();
        map3.put("photo",R.drawable.g3);
        map3.put("name", object[2]);
        data.add(map3);
        Map<String,Object> map4 = new HashMap<String,Object>();
        map4.put("photo",R.drawable.g4);
        map4.put("name", object[3]);
        data.add(map4);
        Map<String,Object> map5 = new HashMap<String,Object>();
        map5.put("photo",R.drawable.g5);
        map5.put("name", object[4]);
        data.add(map5);

        lv.setAdapter(new SimpleAdapter(this, data, R.layout.songslistview,
                new String[]{"photo", "name"}, new int[]{R.id.iv_photo, R.id.tv_name}));
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.i("zhangqi", "position的位置是" + position);
                Intent intent2 = new Intent(MainActivity.this,MusicService.class);
                intent2.putExtra("position",position);
                startService(intent2);
            }
        });
    }

    class MusicServiceConn implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mi= (MusicInterface) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }




    public void play(View view) {

        mi.play();
    }

    public void pause(View view) {
        mi.pause();
    }

    public void continuePlay(View view) {
        mi.continuePlay();
    }

    public void exit(View view) {
        unbindService(conn);
        stopService(intent);
    }
}
